class Solution {
public:
    int in[2001] = { 0 };
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites)
    {
        queue<int> q;
        vector<int> ans;
        vector<vector<int>> edges(numCourses);
        for (auto& e : prerequisites)
        {
            edges[e[0]].push_back(e[1]);
            in[e[1]]++;
        }
        for (int i = 0; i < numCourses; i++)
        {
            if (in[i] == 0)q.push(i);
        }
        while (!q.empty())
        {
            int tmp = q.front();
            q.pop();
            ans.push_back(tmp);
            for (auto& e : edges[tmp])
            {
                if (--in[e] == 0)q.push(e);
            }
        }

        return ans.size() != numCourses ? vector<int>() : vector<int>(ans.rbegin(), ans.rend());
    }
};
};